package net.vkits.controller;

import net.vkits.anjiamiao.application.ContractApplicationService;
import net.vkits.anjiamiao.application.OrderApplicationService;
import net.vkits.anjiamiao.domain.model.Order;
import net.vkits.common.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 2016/12/18.
 */
@Controller
@RequestMapping("/orders")
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Resource
    private OrderApplicationService orderApplicationService;
    @Resource
    ContractApplicationService contractApplicationService;

    @RequiresPermissions("orders:view")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView orders() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "payrecord");
        modelMap.put("sidebar", "orders");
        modelMap.put("currentUser",username);
        return new ModelAndView("/orders", modelMap);
    }

    @RequiresPermissions("orders:view")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Page<Order> getOrders(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable) {
        Page<Order> orders = orderApplicationService.getOrders(pageable);
        logger.info("size is: {}", orders.getTotalElements());
        return orders;
    }

    @RequiresPermissions("orders:modify")
    @RequestMapping(value ="/{id}", method = RequestMethod.GET)
    public ModelAndView order(@PathVariable String id) {
        logger.info("id: {}", id);
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        Order order = orderApplicationService.getOrder(id);
        ModelMap modelMap = new ModelMap();
        modelMap.put("order", order);
        modelMap.put("treeview", "payrecord");
        modelMap.put("sidebar", "orders");
        modelMap.put("currentUser", username);
        return new ModelAndView("/order", modelMap);
    }

    @RequiresPermissions("orders:modify")
    @RequestMapping(value ="/update", method = RequestMethod.POST)
    public String contractUpdate(@RequestParam(value = "orderId", required = true) String orderId,
                                 @RequestParam(value = "status", required = true) String status) {
        logger.info("orderId: {}", orderId);
        orderApplicationService.changeStatus(orderId, status);
        return "redirect:/orders";
    }
}
